/** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
**/


var RSS = {
 	enableMiniView: true,
	
	getTabsFromRss: function(xml) {
		var channels = xml.getElementsByTagName("channel");
		
		if(RSS.enableMiniView)
			miniViewItems = [];
		
		if(!channels) return;
		
		var tabsArray = [];
		
		var l = channels.length;
		
		for (var i = 0; i < l; i++) {
			var channel = channels[i];
			
			var info = RSS.getNodeValue(channel, "info");
			if(!info) info = RSS.getNodeValue(channel, "time");
			var obj = {
				isChannel: true,
				id: channel.getAttribute("id"),
				updateinterval: channel.getAttribute("updateinterval"),
				info: info,
				title: RSS.getNodeValue(channel, "title"),
				description: RSS.getNodeValue(channel, "description"),
				image: RSS.getNodeValue(channel, "image"),
				items: RSS.getItems(channel, tabsArray),
				miniview: RSS.getNodeValue(channel, "miniview" ),
				genres: RSS.getGenres(channel),
				type: RSS.getNodeValue(channel, "type" ),
				videos: RSS.getVideos(channel),
				ad: RSS.getAd(channel)								
			};	
			
			if(RSS.enableMiniView && obj.miniview && obj.miniview=="true" ) {
				miniViewItems.push( obj );
			}
			
			RSS.addItemIntoTabsArray(obj, tabsArray);
		}

		return tabsArray.sort(RSS.sortTabs);
	},
	
	addItemIntoTabsArray: function(item, tabsArray) {
		if(!item.genres) return;
		
		for(var j=0; j<item.genres.length; j++) {
			var g = item.genres[j];
			
			var tab = null;
			for(var k=0; k<tabsArray.length; k++) {
				var t = tabsArray[k];
				if(t.title == g.title) {
					tab = t;
					break;
				}
			}
			
			if(!tab) {
				tab = {
					title: g.title,
					order: g.order,
					items: []
				};
				tabsArray.push(tab);
			}
			
			if(g.order!=0) tab.order = g.order;
			
			tab.items.push(item);
		}
	},
	
	sortTabs: function (a,b) {
		if(a.order == b.order) return a.title > b.title ? 1 : -1;
		if(a.order <= 0 && b.order <= 0) return a.order > b.order ? -1 : 1;
		if(a.order <= 0 && b.order > 0) return 1;
		if(a.order > 0 && b.order <= 0) return -1;
		return a.order < b.order ? -1 : 1;
	},
	
	getNodeValue: function(item, nodeName) {
		var items = item.getElementsByTagName(nodeName);
		if(items && items.length && items[0].firstChild && items[0].parentNode == item) {
			return items[0].firstChild.nodeValue;
		}
		
		return null;
	},
	
	getNodeAttribute: function(item, nodeName, attr) {
		var items = item.getElementsByTagName(nodeName);
		if(items && items.length && items[0].getAttribute(attr)) {
			return items[0].getAttribute(attr);
		}
		
		return null;
	},
	
	getGenres: function(item) {
		var genres = item.getElementsByTagName("label");
		if(!genres || !genres.length) genres = item.getElementsByTagName("genre");
		if(!genres) return [];
		
		var genreArray = [];
		
		var l = genres.length;
		for(var i=0; i<l; i++) {
			var genre = genres[i];
			
			if(genre.parentNode != item) continue;
			
			if(genre.firstChild && genre.firstChild.nodeValue) {
				genreArray.push({
					order: genre.getAttribute("order") ? parseInt(genre.getAttribute("order"), 10) : 0,
					title: genre.firstChild.nodeValue
				});
			}
		}
		
		return genreArray;
	},
	
	getItems: function(root, tabsArray) {
		var items = root.getElementsByTagName("item");
		if(!items) return [];
		
		var itemArray = [];
		
		var l = items.length;
		for(var i=0; i<l; i++) {
			var item = items[i];
			
			if(item.parentNode != root) continue;
			
			var info = RSS.getNodeValue(item, "info");
			if(!info) info = RSS.getNodeValue(item, "time");
			var obj = {
				miniview: RSS.getNodeValue(item, "miniview"),
				image: RSS.getNodeValue(item, "image"),
				title: RSS.getNodeValue(item, "title"),
				description: RSS.getNodeValue(item, "description"),
				info: info,
				genres: RSS.getGenres(item),
				type: RSS.getNodeValue(item, "type" ),
				videos: RSS.getVideos(item),
				ad: RSS.getAd(item),
				duration: RSS.getNodeValue(item, "duration")
			};
			
			if(RSS.enableMiniView && obj.miniview && obj.miniview=="true") {
				miniViewItems.push( obj );
			}
			
			itemArray.push(obj);
			
			RSS.addItemIntoTabsArray(obj, tabsArray);
		}
		
		return itemArray;
	},

	getVideos: function(item) {
		var obj = [];
		var videos = item.getElementsByTagName("link");
		var v = videos.length;
		for(var i=0; i<v; i++) {
			var video = videos[i];			
			if(video.parentNode != item) continue;

			obj.push( {
				link: video.firstChild.nodeValue,
				bitrate: video.getAttribute("bitrate")
			} );
		}
		
		return obj;
	},	
	
	getAd: function(item) {
		var obj = null;
		var ads = item.getElementsByTagName("ad");
		if(ads && ads.length) {
			var ad = ads[0];
			obj = {
				video: {
					link: RSS.getNodeValue(ad, "link"),
					bitrate: RSS.getNodeAttribute(ad, "link", "bit")
				},
				backLink: RSS.getNodeValue(ad, "backLink")
			};
		}
		
		return obj;
	},	
	
	findGenre: function(genres, title, order) {
		for(var i=0; i<genres.length; i++) {
			var genre = genres[i];
			if(genre.title == title) {
				return genre;
			}
		}
		
		return null;
	},
	
	search: function(tabsArray, search_text, max_results) {
		search_text = search_text.toLowerCase();
		var ret = [];
		for(var i=0; i<tabsArray.length; i++) {
			var tab = tabsArray[i];
			var items = tab.items; 
			
			for(var j=0; j<items.length; j++) {
				var item = items[j];
				if((item.title && item.title.toLowerCase().indexOf(search_text)!=-1) || 
					(item.description && item.description.toLowerCase().indexOf(search_text)!=-1))
					//if(!jQuery.inArray(item, items))
						ret.push(item);
						
			}
		}
		
		ret = jQuery.unique(ret);
		
		if(max_results) {
			return ret.slice(0, max_results);
		}
		return ret;
	},
	
	compare: function(item1, item2) {		
		for(var p in item1) {
			if(item1.hasOwnProperty(p)) {
				if(item2[p] == undefined) {
					return false;
				}
				if(item1[p]!==item2[p])
					return false;
			}
		}
		
		return true;
	}
 }
